#! /usr/bin/perl -w
use strict;

use Dpkg::Deps;

# Get the first in a list of or-ed dependencies.
sub first_dep ($) {
	my $inner = shift;
	while ($inner->isa('Dpkg::Deps::OR')) {
		my @deps = $inner->get_deps();
		$inner = $deps[0];
	}
	unless ($inner->isa('Dpkg::Deps::Simple')) {
		die sprintf "Couldn't reduce $inner to a simple dependency!\n";
	}
	return $inner;
}

my $control = 'debian/control';
unless (-e $control) {
	$control = '../debian/control';
	unless (-e $control) {
		die "cannot find debian/control";
	}
}

my %builddeps;

# Always include base lingmo-installer build dependencies.
my @basebd = (
    'adwaita-icon-theme',
    'apt',
    'autopoint',
    'dctrl-tools',
    'debconf-utils',
    'debhelper (>= 9)',
    'dh-autoreconf',
    'dh-di (>= 3)',
    'dh-python',
    'dpkg-dev (>= 1.14.4)',
    'gir1.2-nma-1.0',
    'gir1.2-soup-2.4',
    'gir1.2-timezonemap-1.0',
    'gir1.2-webkit2-4.0',
    'intltool (>= 0.40.0)',
    'imagemagick',
    'libcairo2-dev',
    'keymapper (>= 0.5.3-7)',
    'libgirepository1.0-dev',
    'libglib2.0-dev',
    'libgtk-3-dev (>= 3.20)',
    'libindicator3-dev',
    'liblocale-gettext-perl',
    'librsvg2-bin',
    'gobject-introspection',
    'pkg-config',
    'po-debconf (>= 1.0)',
    'pycodestyle',
    'pyflakes3 (>= 0.7.2)',
    'python-gi-dev',
    'python3-all (>= 3.1)',
    'python3-apt (>= 0.7.100.3~)',
    'python3-cairo',
    'python3-dbus',
    'python3-debconf',
    'python3-gi',
    'python3-gi-cairo',
    'python3-icu (>= 1.0)',
    'python3-mock (>= 0.7.0)',
    'python3-pam',
    'qrencode',
    'rename',
    'scour',
    'gir1.2-xkl-1.0',
    'lingmo-artwork',
    'udev',
    'xkb-data (>= 0.9)',
    'xvfb',
);
my $basebd = join ', ', @basebd;
my $parsed = Dpkg::Deps::deps_parse($basebd, reduce_arch => 1);
for my $bd ($parsed->get_deps()) {
	my $firstbd = first_dep($bd);
	$builddeps{$firstbd->{package}} = $firstbd;
}

for my $source (<source/*/debian/control>) {
	# We don't build console-setup, so skip its build-dependencies.
	next if $source eq 'source/console-setup/debian/control';
	open SOURCE, '<', $source or die "can't open $source: $!";
	while (<SOURCE>) {
		if (/^Build-Depends(?:-Indep)?:\s*(.*)/i) {
			$parsed = Dpkg::Deps::deps_parse($1, reduce_arch => 1);
			for my $bd ($parsed->get_deps()) {
				# Reduce any or-ed dependencies to the first
				# alternative.
				my $firstbd = first_dep($bd);
				my $package = $firstbd->{package};
				if (not exists $builddeps{$package} or
				    $firstbd->implies($builddeps{$package})) {
					$builddeps{$package} = $firstbd;
				}
			}
			next;
		}
	}
	close SOURCE;
}

my $builddeps =
	join ",\n               ", map { "$builddeps{$_}" } sort keys %builddeps;
open IN, '<', $control or die "can't open $control: $!";
open OUT, '>', "$control.tmp" or die "can't open $control.tmp for writing: $!";
my $in_build_depends = 0;
foreach (<IN>) {
	if ($in_build_depends) {
		if (!/^.+:/) {
			next;
		} else {
			$in_build_depends = 0;
		}
	}

	if (/^(Build-Depends:\s*)(.*)/) {
		print OUT "Build-Depends: $builddeps\n";
		$in_build_depends = 1;
	} else {
		print OUT or die "can't print to $control.tmp: $!";
	}
}
close OUT or die "can't close $control.tmp: $!";
close IN;
rename "$control.tmp", $control
	or die "can't rename $control.tmp to $control: $!";
